var ContractList = function() {
	// 添加联系人
	var addLinkMan = function() {
		$("#addLinkManBtn")
				.bind(
						'click',
						function() {
							var records = $(this).parent().prev("table").find(
									"tbody").find("tr");
		   					var genderHtml = "<select name='gender' style='width:50px' >"
							+"<option value=0 > </option>"
							+"<option value=1 >男</option>"
							+"<option value=2 >女</option></select>";
							if (records.size() == 0) {
								var rowContenthtml = "<tr>"
										+ "<td><div class='float_left'><div class='form_row'><div class='form_field'><label><input name='linkman' value='' class='textfield pd_5x2px w_56px'  /><input type='hidden' name='pk_linkman' value='' /></label></div></div></div></td>"
										+ "<td><div class='float_left'><div class='form_row'><div class='form_field'><label><input name='tel' value='' class='textfield pd_5x2px w_126px' ></label></div></div></div></td>"
										+ "<td><div class='float_left'><div class='form_row'><div class='form_field'><label><input name='mobile' value='' class='textfield pd_5x2px w_86px' ></label></div></div></div></td>"
										+ "<td><div class='float_left'><div class='form_row'><div class='form_field'><label><input name='email' value='' class='textfield pd_5x2px w_178px' ></label></div></div></div></td>"
										+ "<td><div class='float_left'><div class='form_row'><div class='form_field'><label><input name='position' value='' class='textfield pd_5x2px w_106px' ></label></div></div></div></td>"
										+ "<td><div class='float_left'><div class='form_row'><div class='form_field'><label>"+genderHtml+" </label></div></div></div></td>"
										+ "<td class='last_column'><a id='deleteLinkManBtn"
										+ (records.size() + 1)
										+ "' class='btn_delete mrg_0x25px' style='cursor:pointer'></a></td>"
										+ "</tr>";
								$(this).parent().prev("table").find("tbody")
										.append(rowContenthtml);
							}

							if (records.size() > 0) {
								var prevRecord = records.last();
								var newRecord = prevRecord.clone();
								newRecord.find("input").each(function() {
									$(this).val("");
								});
								newRecord.insertAfter(prevRecord);
								var idx = records.length + 1;
								newRecord.find(".first_column").html(idx);
								newRecord.find("[name='gender']").val(0);
							}
							return false;
						});
	};

	// 取消联系人
	var cancel = function() {
		$("#linkManCancel").bind('click', function() {
			window.location = ebvp_context + "/selfinfo/contractlist";
		});
	};

	// 提交前校验
	var checkLinkMan = function() {
		
		$("body")
			.find("tbody").find(".error").remove();
		
		var errCount = 0;
		var ErrMsg = getLanMsg("0ec22180-100301");
		var path = ebvp_context + "/commonres/common/frame/src/img/validate_error.png";
		
		$("body")
				.find("tbody")
				.find("tr")
				.each(
						function() {
							var linkmanNode = $(this).find("[name='linkman']");
							var telNode = $(this).find("[name='tel']");
							var mobileNode = $(this).find("[name='mobile']");
							var emailNode = $(this).find("[name='email']");
							// 联系人非空
							if (linkmanNode.val() == null
									|| linkmanNode.val() == "") {
								var img = $("<img></img>")
										.attr("src", path)
										.attr("for", "linkman")
										.attr("title", ErrMsg)
										.attr("style",
												"position:absolute; width: 18px; height: 18px; ").attr("class", "error");
								img.insertAfter(linkmanNode);
								var position = linkmanNode.offset();
								position.left += 40;
								img.offset(position);
								
								errCount = errCount + 1;
							}
							//电话非空
							if(telNode.val() == null || telNode.val() == ""){
								var img = $("<img></img>")
										.attr("src", path)
										.attr("for", "tel")
										.attr("title", ErrMsg)
										.attr("style",
												"position:absolute; width: 18px; height: 18px;").attr("class", "error");
								img.insertAfter(telNode);
								
								var position = telNode.offset();
								position.left += 112;
								img.offset(position);
								
								errCount = errCount + 1;
							}
							/*else if(!checkTel(telNode.val())){
								var telErrMsg = getLanMsg("0ec22180-100302");
								var img = $("<img></img>")
									.attr("src", path)
									.attr("for", "tel")
									.attr("title", telErrMsg)
									.attr("style",
											"position:absolute; width: 18px; height: 18px;").attr("class", "error");
								img.insertAfter(telNode);
								
								var position = telNode.offset();
								position.left += 172;
								img.offset(position);
								
								errCount = errCount + 1;
							}*/
							//手机非空
							if(mobileNode.val() == null || mobileNode.val() == "" ){
								var img = $("<img></img>")
										.attr("src", path)
										.attr("for", "mobile")
										.attr("title", ErrMsg)
										.attr("style",
												"position:absolute; width: 18px; height: 18px;").attr("class", "error");
								img.insertAfter(mobileNode);
								
								var position = mobileNode.offset();
								position.left += 70;
								img.offset(position);
								
								errCount = errCount + 1;
							}
//							else if(!checkMobile(mobileNode.val())){
//								var mobileErrMsg = getLanMsg("0ec22180-100303");
//								var img = $("<img></img>")
//									.attr("src", path)
//									.attr("for", "mobile")
//									.attr("title", mobileErrMsg)
//									.attr("style",
//											"position:absolute; width: 18px; height: 18px;").attr("class", "error");
//								img.insertAfter(mobileNode);
//								
//								var position = mobileNode.offset();
//								position.left += 172;
//								img.offset(position);
//								
//								errCount = errCount + 1;
//							}
							//邮箱非空
							if(emailNode.val() == null || emailNode.val() == "" ){
								var img = $("<img></img>")
										.attr("src", path)
										.attr("for", "email")
										.attr("title", ErrMsg)
										.attr("style",
												"position:absolute; width: 18px; height: 18px; ").attr("class", "error");
								img.insertAfter(emailNode);
								
								var position = emailNode.offset();
								position.left += 162;
								img.offset(position);
								
								errCount = errCount + 1;
							}else if(!checkEmail(emailNode.val())){
								var emailErrMsg = getLanMsg("0ec22180-100304");
								var img = $("<img></img>")
										.attr("src", path)
										.attr("for", "email")
										.attr("title", emailErrMsg)
										.attr("style",
												"position:absolute; width: 18px; height: 18px; ").attr("class", "error");
								img.insertAfter(emailNode);
								
								var position = emailNode.offset();
								position.left += 180;
								img.offset(position);
								
								errCount = errCount + 1;
							}
						});
		if(errCount > 0){
			return false;
		}else{
			return true;
		}
	};

	// 提交联系人
	var submitLinkMan = function() {
		$("#linkManSubmit").bind(
				'click',
				function() {
					if(!checkLinkMan()){
						return ;
					}
					$("#linkmanForm").attr("action",
							ebvp_context + "/selfinfo/saveLinkMan");
					$("#linkmanForm").submit();
		});
	};

	this.init = function() {
		addLinkMan();
		cancel();
		submitLinkMan();

		$("body").delegate(".btn_delete", "click", function() {
			if (confirm(getLanMsg("0ec22180-100300"))) {
				$(this).parents("tr").remove();
			}
		});
	};
};

/**
 * 
 */
$(function() {
	var contractList = new ContractList();
	contractList.init();
});